package jp.aist.gtrc.plus.scheduler.job;

import java.util.Comparator;
import java.util.Map;

import jp.aist.gtrc.plus.scheduler.job.FairshareScheduler;
import jp.aist.gtrc.plus.scheduler.status.JobStatus;
import jp.aist.gtrc.plus.scheduler.util.SortKey;

public class FairshareSortKey implements SortKey<JobStatus>, Comparator<JobStatus> {
   private Map<String,Integer> cpuUsage;

   public FairshareSortKey(FairshareScheduler scheduler) {
      this.cpuUsage = scheduler.getCPUUsageMap();
   } 

   public Comparator<JobStatus> getComparator() {
      return this;
   }

   public int compare(JobStatus j1, JobStatus j2) {
      Integer cpuUsage1 = cpuUsage.get(j1.getOwner());
      if (cpuUsage1 == null) {
         cpuUsage1 = new Integer(0);
      }
      Integer cpuUsage2 = cpuUsage.get(j2.getOwner());
      if (cpuUsage2 == null) {
         cpuUsage2 = new Integer(0);
      }
      return cpuUsage1 - cpuUsage2;
   }
}

